package com.xieyong.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class Leetcode3169 {
    public static void main(String[] args) {
        Leetcode3169 solution = new Leetcode3169();
        int days = 10;
        int[][] meetings = {{4,7},{1,6},{9,10}};
        int i = solution.countDays(days, meetings);
        System.out.println(i);
    }


    public int countDays(int days, int[][] meetings) {
        Arrays.sort(meetings,(a,b)->a[0]-b[0]);
        int leftday = 1, rightday = 0;
        for (int[] meeting : meetings) {
            if (meeting[0] > rightday) {
                days -= (rightday - leftday + 1);
                leftday = meeting[0];
            }
            rightday = Math.max(meeting[1], rightday);
        }
        days -= (rightday - leftday + 1);
        return days;
    }
//    public int countDays(int days, int[][] meetings) {
////        合并会议
//        List<int[]> meetingList = new ArrayList<>();
//        Arrays.sort(meetings, (o1, o2) -> o1[0] - o2[0]);
//        meetingList.add(meetings[0]);
//        int index = 0;
//        for (int i = 1; i < meetings.length; i++) {
//            int[] last = meetingList.get(index);
//            if(last[1] >= meetings[i][0]) {
//                meetingList.set(index, new int[]{last[0], Math.max(last[1], meetings[i][1])});
//            } else {
//                meetingList.add(meetings[i]);
//                index++;
//            }
//        }
////        for (int[] ints : meetingList) {
////            System.out.println(ints[0] + " " + ints[1]);
////        }
////        计算所有的会议时间
//        int mTime = 0;
//        for (int[] meeting : meetingList) {
//            mTime += meeting[1] - meeting[0] + 1;
//        }
//        return days - mTime;
//    }
}
